package com.qinglei.recoup.longconn.dispatch.controller;

import com.qinglei.recoup.longconn.dispatch.service.DispatchService;
import com.qinglei.recoup.longconn.dispatch.util.RadarJsonUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("dispatch")
public class DispatchController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${dispatch.fpathMove:}")
    private String fpathMove;

    @Value("${dispatch.fpathBreath:}")
    private String fpathBreath;

    @Value("${dispatch.fpathSleepReport:}")
    private String fpathSleepReport;

    @Value("${dispatch.fpathHealth:}")
    private String fpathHealth;

    @Value("${dispatch.fpathSleepAnalysis:}")
    private String fpathSleepAnalysis;

    @Autowired
    private DispatchService dispatchService;

    @GetMapping("/fpathSleepAnalysis")
    //调用第2个任务ss 睡眠定时任务成功---->调用体动检测程序
    public Map<String, Object> fpathMove(String flag, String fpath) {
        log.info("调用第2个任务ss通知 -------睡眠定时任务成功---->调用体动检测程序");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if ("0".equals(flag) && fpath != null) {
                log.info("开始向算法服务发送请求, flag:{}, fpath:{}", flag, fpath);
                ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathMove, String.class, fpath);
                result.put("status", "0");
                result.put("message", "调用算法接口成功");
                result.put("fpath", fpath);
                return result;
            } else {
                result.put("status", "-1");
                result.put("message", "fpath为空，或flag为-1或空，请假查输入参数是否正确");
                result.put("fpath", fpath);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "-2");
            result.put("message", "调用算法失败请检查算法服务器是否启动");
            result.put("fpath", fpath);
            return result;
        }
    }

    //
    @GetMapping("/fpathMove")
    //调用第3个任务hj通知 体动检测程序调用成功---->调用呼吸慢病程序
    public Map<String, Object> fpathBreath(String fpath, String flag) {
        log.info("调用第3个任务hj通知 --------体动检测程序调用成功---->调用呼吸慢病程序");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if ("0".equals(flag) && fpath != null) {
                log.info("开始向算法服务发送请求, flag:{}, fpath:{}", flag, fpath);
                ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathBreath, String.class, fpath);
                result.put("status", "0");
                result.put("message", "调用算法接口成功");
                result.put("fpath", fpath);
                return result;
            } else {
                result.put("status", "-1");
                result.put("message", "fpath为空，或flag为-1或空，请假查输入参数是否正确");
                result.put("fpath", fpath);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "-2");
            result.put("message", "调用算法失败, 请检查算法服务器是否启动");
            result.put("fpath", fpath);
            return result;
        }

    }

    //
    @GetMapping("/fpathBreath")
    //调用第3个任务hj通知 体动检测程序调用成功---->调用呼吸慢病程序
    public Map<String, Object> fpathSleepReport(String fpath, String flag) {
        log.info("调用第3个任务hj通知 --------体动检测程序调用成功---->调用呼吸慢病程序");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if ("0".equals(flag) && fpath != null) {
                log.info("开始向算法服务发送请求, flag:{}, fpath:{}", flag, fpath);
                ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathSleepReport, String.class, fpath);
                result.put("status", "0");
                result.put("message", "调用算法接口成功");
                result.put("fpath", fpath);
                return result;
            } else {
                result.put("status", "-1");
                result.put("message", "fpath为空，或flag为-1或空，请假查输入参数是否正确");
                result.put("fpath", fpath);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "-2");
            result.put("message", "调用算法失败, 请检查算法服务器是否启动");
            result.put("fpath", fpath);
            return result;
        }

    }

    @GetMapping("/fpathSleepReport")
    //调用第4个任务健康报告
    public Map<String, Object> fpathHealth(String fpath, String flag) {
        log.info("调用第4个任务kx通知 --------呼吸慢病调用成功---->执行健康报告逻辑");

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if ("0".equals(flag) && fpath != null) {
                log.info("开始向算法服务发送请求, flag:{}, fpath:{}", flag, fpath);
                ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathHealth, String.class);
                result.put("status", "0");
                result.put("message", "调用算法接口成功");
                result.put("fpath", fpath);
                return result;
            } else {
                result.put("status", "-1");
                result.put("message", "fpath为空，或flag为-1或空，请假查输入参数是否正确");
                result.put("fpath", fpath);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("status", "-2");
            result.put("message", "调用算法失败, 请检查算法服务器是否启动");
            result.put("fpath", fpath);
            return result;
        }
    }

    @GetMapping("/test")
    //调用第4个任务健康报告
    public String fpathHealth() throws IOException {
        log.info("进来了====================================================================================");
        log.info("进来了====================================================================================");
        //dispatchService.DispatchJob().run();
        List<String> allTadayJson = RadarJsonUtil.getAllTadayJson();
        LocalDate  date =LocalDate.now();
        File file = new File("/opt/app/recoup/filepath/"+date+".json");
        FileWriter writer = new FileWriter(file);
        JSONArray fpath = JSONArray.fromObject(allTadayJson);
        writer.write(fpath.toString());
        writer.close();
        String filepath = file.getAbsolutePath();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathSleepAnalysis,String.class,filepath);
        log.info(file.getAbsolutePath()+"");
        return "file"+file.getAbsolutePath();
    }



}
